﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WanMeiCommon;
using WanMeiModel;

namespace WanMeiService
{
    public class ChatHub : BaseHub
    {
        /// <summary>
        /// 客户端请求发送消息
        /// </summary>
        /// <param name="parmsModel"></param>
        public void SendMsg(SendModel parmsModel)
        {
            try
            {
                //--------验证开始
                UserInfoModel sendUserInfo = PublicDataInfomation.Instance.GetUser(parmsModel.SendConnectionId);
                //验证发送人连接状态
                if (sendUserInfo == null)
                {
                    base.NoticeMsg("连接异常",parmsModel.SendConnectionId);
                    return;
                }
                //如果是私聊，验证接收者，是否刷新过网页，是否下线等等因素
                if (!string.IsNullOrEmpty(parmsModel.ToConnectionId))
                {
                    if (PublicDataInfomation.Instance.GetUser(parmsModel.ToConnectionId) == null)
                    {
                        base.NoticeMsg("对方已离线", parmsModel.SendConnectionId);
                        return;
                    }
                }
                string key = "UserInfoModel_" + sendUserInfo.HeroId + "_" + sendUserInfo.UserName;
                UserInfoModelResult sendUserRole = RedisManager.Get<UserInfoModelResult>(key);
                //验证发送人角色信息
                if (sendUserRole == null)
                {
                    base.NoticeMsg("角色信息异常", parmsModel.SendConnectionId);
                    return;
                }
                //--验证敏感词-》待做

                //--------验证结束
                //异步消息日志
                new MainController().WriteChatLog(new ChatLogModel()
                {
                    ChatType = parmsModel.ChatType,
                    HeroId = sendUserInfo.HeroId,
                    Nick = sendUserRole.UserRoleModel.RoleNick,
                    SendMsg = parmsModel.SendMsg,
                    Un = sendUserInfo.UserName
                });
                //构造消息通知模型
                ReceiveModel relModel = new ReceiveModel();
                relModel.SendConnectionId = parmsModel.SendConnectionId;
                relModel.ChatType = parmsModel.ChatType;
                relModel.SendMsg = parmsModel.SendMsg;
                relModel.SendRoleLevelTitle = sendUserRole.UserRoleModel.LevelTitle;
                relModel.SendRoleLevelVip = sendUserRole.UserRoleModel.VipLevel;
                relModel.SendRoleName = sendUserRole.UserRoleModel.RoleNick;
                relModel.GameLevel = sendUserRole.UserRoleModel.GameLevel;
                relModel.Un = sendUserInfo.UserName;
                relModel.ToRoleName = parmsModel.ToRoleName;
                relModel.HeroId = sendUserInfo.HeroId;
                switch ((ChatType)parmsModel.ChatType)
                {
                    case ChatType.All:
                        base.SendMsgReceiveForAll<ReceiveModel>(true, relModel);
                        break;
                    case ChatType.Single:
                        var noticeList = new List<string>();
                        noticeList.Add(parmsModel.SendConnectionId);
                        noticeList.Add(parmsModel.ToConnectionId);
                        base.SnedMsgReceiveForPrivate(true, relModel, noticeList);
                        break;
                }
            }
            catch (Exception ex)
            {
                LogManger.Instance.WriteLog("发送消息异常" + ex.ToString(), "ChatHub");
            }
           
        }
    }
}
